﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
using System.Collections.Generic;
using System.Xml;

namespace Coldairarrow.Util
{
    /// <summary>
    /// XML文档操作帮助类
    /// </summary>
    public class XmlHelper
    {
        /// <summary>
        /// 序列化为XML字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            Type type = obj.GetType();
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            try
            {
                //序列化对象
                xml.Serialize(Stream, obj);
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

            sr.Dispose();
            Stream.Dispose();

            return str;
        }

        /// <summary>
        /// 获取xml里面的数据，将数据已list形式返回
        /// </summary>
        /// <returns></returns>
        public static List<node_model> GetXMLList()
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(@"wwwroot/Displayuser.xml", settings);
            xmlDoc.Load(reader);

            // 得到根节点bookstore
            XmlNode xn = xmlDoc.SelectSingleNode("UserIps");

            // 得到根节点的所有子节点
            XmlNodeList xnl = xn.ChildNodes;

            List<node_model> xml_list = new List<node_model>();

            foreach (XmlNode xn1 in xnl)
            {
                node_model xmlmode = new node_model();
                // 将节点转换为元素，便于得到节点的属性值
                XmlElement xe = (XmlElement)xn1;

                // 得到Book节点的所有子节点
                XmlNodeList xnl0 = xe.ChildNodes;
                xmlmode.Type = xe.GetAttribute("type").ToString();
                xmlmode.IpAddress = xnl0.Item(0).InnerText;
                xml_list.Add(xmlmode);
            }
            reader.Close();

            return xml_list;
        }

        /// <summary>
        /// 遍历list集合，判断ip是否为白名单ip如果是则通过
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ipadress"></param>
        /// <returns></returns>
        public static bool IsIpdisable(List<node_model> list, string ipadress)
        {
            //默认为不是白名单用户，只有白名单用户才可以访问
            bool istrue = false;

            for (int i = 0; i < list.Count; i++)
            {
                //判断ip地址是否一样，并且在白名单中
                if (list[i].IpAddress != ipadress || list[i].Type == "WhiteUser")
                {
                    istrue = true;
                }
                else
                {
                    istrue = false;
                }
            }
            return istrue;
        }
    }


    public class node_model
    {
        public string IpAddress { get; set; }
        public string Type { get; set; }
    }
}
